package com.zjgxzh.userservice.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.zjgxzh.userservice.mapper.UserMapper;
import com.zjgxzh.userservice.pojo.User;
import com.zjgxzh.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RestTemplate restTemplate;

    public SaTokenInfo userLogin(User user) {
        String password = userMapper.findPasswordByUsername(user.getUsername());
        if (password != null && password.equals(user.getPassword())){
            StpUtil.login(userMapper.findIdByUsername(user.getUsername()));
            return StpUtil.getTokenInfo();
        }
        else return null;
    }

    public boolean userRegister(User user) {
        if (userMapper.findByUsername(user.getUsername()) != null) {
            return false;
        }
        else{
            userMapper.addUser(user);
            return true;
        }
    }

}
